<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input v-model="queryParams.keywords" placeholder="关键字" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="hover" class="data-table">
      <div class="data-table__toolbar">
        <div class="data-table__toolbar--actions">
          <el-button type="primary" plain icon="plus" @click="handleOpenDialog()">新增</el-button>
          <el-button type="danger" :disabled="ids.length === 0" icon="delete" @click="handleDelete()">
            删除
          </el-button>
        </div>
      </div>

      <el-table ref="dataTableRef" v-loading="loading" :data="houseList" highlight-current-row border
        class="data-table__content" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="名称" prop="name" width="150" />
        <el-table-column label="主材种类数" prop="mainMaterialNum" width="120" align="center" />
        <el-table-column label="辅材种类数" prop="subMaterialNum" width="120" align="center" />
        <el-table-column label="定额种类数" prop="normMaterialNum" width="120" align="center" />
        <el-table-column label="标注价格" width="150" align="right" prop="price">
          <template #default="scope">
            {{ `￥${scope.row.price}` }}
          </template>
        </el-table-column>
        <el-table-column label="总成本价" width="150" align="right" prop="totalCostPrice">
          <template #default="scope">
            {{ `￥${scope.row.totalCostPrice}` }}
          </template>
        </el-table-column>
        <el-table-column label="描述" min-width="150" prop="remark" />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button :disabled="scope.row.systemd" type="primary" size="small" link icon="edit"
              @click="gotoItem(scope.row)">
              配置
            </el-button>
            <el-button :disabled="scope.row.systemd" type="danger" size="small" link icon="delete"
              @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="fetchData" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import router from "@/router";

import SealMealAPI, {
  SealMealPageVO,
  SealMealPageQuery,
} from "@/api/business/material/sealMeal.api";
defineOptions({
  name: "SealMeal",
  inheritAttrs: false,
});

const queryFormRef = ref();

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<SealMealPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 客户表格数据
const houseList = ref<SealMealPageVO[]>();

// 跳转到套餐项
function gotoItem(row?: SealMealPageVO) {
  if (row) {
    router.push({
      path: "/saleMealItem",
      query: { saleMealId: row.id, title: "【" + row.name + "】套餐数据" },
    });
  } else {
    router.push({
      path: "/saleMealItem",
      query: { saleMealId: 0, title: "新增套餐数据" },
    });
  }
}

// 获取数据
function fetchData() {
  loading.value = true;
  SealMealAPI.getPage(queryParams)
    .then((data) => {
      houseList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 查询（重置页码后获取数据）
function handleQuery() {
  queryParams.pageNum = 1;
  fetchData();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  fetchData();
}

// 行复选框选中
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

// 新增
async function handleOpenDialog() {
  gotoItem();
}

// 删除
function handleDelete(roleId?: number) {
  const roleIds = [roleId || ids.value].join(",");
  if (!roleIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    SealMealAPI.deleteByIds(roleIds)
      .then(() => {
        ElMessage.success("删除成功");
        handleResetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});
</script>
